package com.itheima.duotai3;

public class Test {
    public static void main(String[] args) {
        /*useAnimal(new Dog());
        useAnimal(new Cat());*/

     /*  Animal animal = getDog();//Animal animal = new Dog();
        animal.eat();
        animal.drink();
        Animal animal1= getCat();//Animal animal= new Cat();
        animal.eat();
        animal.drink();*/
     /*Animal animal= getAnimal("狗");
        animal.eat();
        animal.drink();
     Animal animal2= getAnimal("猫");
        animal2.eat();
        animal2.drink();*/
    useAnimal(new Cat());
    useAnimal(new Dog());

    }
    //定义一个方法 使用 动物
    //参数 动物 返回值类型 void
    public static void useAnimal(Animal animal){
        animal.eat();
        animal.drink();
        if(animal instanceof Dog){
            Dog dog= (Dog) animal;
            dog.watchhome();
        }

    }
    //使用 狗
   /*public static void useAnimal(Dog dog){
        dog.eat();
        dog.drink();
        dog.watchhome();
    }
    //使用猫
    public static void useAnimal(Cat  cat){
        cat.eat();
        cat.drink();
    }*/

    // 定义一个方法 给我 生产一个动物
    /*public static Dog getDog(){
        return new Dog();
    }
    public static Cat getCat(){
        return new Cat();
    }*/
    public static Animal getAnimal(String leixing){
        if(leixing.equals("狗")){
            return new Dog();
        }
        if (leixing.equals("猫")){
            return new Cat();
        }
        return null;
    }
}

abstract class Animal{
    public void drink(){
        System.out.println("喝水");
    }
    public abstract void eat();
}
class Dog extends Animal{
    @Override
    public void eat() {
        System.out.println("狗吃肉");
    }
    public void watchhome(){
        System.out.println("狗看家");
    }
}
class Cat extends Animal{

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
    public void sleep(){
        System.out.println("一天睡一天");
    }
}

